<template>
	<view class="scrool-page">
		<view class="list-cont">
			<scroll-view  class="scrool-more" style="height: 100%" scroll-y="true" scroll-with-animation="true">
				<view v-for="(item,index) in productList" :key="index">
					<uni-card :title="'序号:'+(index+1)">
						<text class="uni-body">箱码：{{ item.boxCode }}</text><br>
					</uni-card>
				</view>
			</scroll-view>
		</view>
				
		<view class="footer">
			<button type="default" size='mini' @click="reduceData" style="width:48%; margin-left:1%;" v-if="!userflag">减码</button>
			<button type="default" size='mini' @click="reduceData" style="width:48%; margin-left:1%;" v-else>加箱</button>
			<button type="default" size='mini' @click="submit" style="width:49%; margin-left:1%;">完成</button>
		</view>
	</view>
	
</template>

<script>
	import { getToken } from '@/utils/auth';
	import common from '../../pages/common/common.js'
	var main, receiver, filter;
	var ScanDeviceClass = plus.android.importClass("android.device.ScanDevice");
	var scanDevice;
	export default {
		data() {
			return {
				pkOrder:'',
				id:'',
				productList:[],
				locationCode:'',
				userName:this.$store.state.user.name,
				baseUrl:getApp().globalData.config.baseUrl,
				orgId:getApp().globalData.config.orgId,
				userflag:false,
			}
		},
		
		onLoad: function (option) { //option为object类型，会序列化上个页面传递的参数
			this.orgId = uni.getStorageSync('orgId');
			this.pkOrder = option.pkOrder;
			this.id = option.id;
			uni.setNavigationBarTitle({
				title:option.title
			});
		},
		
		onBackPress(options) {
			var that = this;
			if (options.from === 'navigateBack') {
			     return false;
			}
			//that.unregisterScan();
			uni.navigateBack({  //uni.navigateTo跳转的返回，默认1为返回上一级
			    delta: 1
			});
			return true;
		},
		
		onShow() {
			this.orgId = uni.getStorageSync('orgId');
			scanDevice = new ScanDeviceClass();
			scanDevice.openScan(); // 打开扫描
			scanDevice.setOutScanMode(0); // 扫描模式				
			this.initScan();
			this.registerScan();
		},
		
		onHide: function() {
			this.unregisterScan();
		},
		
		destroyed: function() {
			this.unregisterScan();
		},
		
		methods: {			
			initScan() {
				let _this = this;
				main = plus.android.runtimeMainActivity(); //获取activity  
				var IntentFilter = plus.android.importClass('android.content.IntentFilter');
				filter = new IntentFilter();
				filter.addAction("scan.rcv.message"); // 换你的广播动作  
				receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
					onReceive: function(context, intent) {
						plus.android.importClass(intent);
						let code = intent.getByteArrayExtra('barocode');
					    let codeStr = common.utf8ByteToUnicodeStr(code);
						scanDevice.stopScan(); // 停止扫描	
						_this.buttonScan(codeStr);
						_this.viewScan(codeStr);											
					}
				});				
			},
			
			registerScan() {
				main.registerReceiver(receiver, filter);
			},
			
			unregisterScan() {
				main.unregisterReceiver(receiver);
			},
			
			buttonScan(codeStr){
				var that = this;
				if(that.userflag){
					return;
				}
				var result = common.scanCode(codeStr);
				if(result[0] == "K"){
					that.locationCode = result[1];
				}else if(result[0] == "C"){
					if(that.locationCode == null || that.locationCode == ''){//一定是先扫描库区
						uni.showToast({
							title: '请先扫描库区',
							icon: 'none',
							duration: 3000
						});
						return;
					}
					var flag = false;
					for(var i=0;i<that.productList.length;i++){
						if(that.productList[i].boxCode == result[1]){
							flag = true;
							uni.showToast({
								title: '此箱码已经扫描',
								icon: 'none',
								duration: 3000
							});
							return;
						}
					}
					if(!flag){
						uni.request({
							url:that.baseUrl+'/system/barcode/box',
							data:{
								userName:that.userName,
								boxCode:result[1]	,
								orgId:that.orgId,
							},
							method: 'POST',
							header:{
								'authorization':  getToken()
							},
							dataType:'json',
							success: (res) =>{								
								if(res.data.code == '200' || res.data.code == 200) {
									var storageLocation = res.data.data.storageLocation;
									var barcodeId = res.data.data.barcodeId;
									if(storageLocation != that.locationCode){
										uni.showToast({
											title: '产品库位与扫描的不一致,请检查',
											icon: 'none',
											duration: 3000
										});
										return;
									}else{
										var arr={boxCode:result[1],storageLocation:that.locationCode,barcodeId:barcodeId};
										that.productList.push(arr);
									}
								}else{
									uni.showToast({
										title: res.data.msg,
										icon: 'none',
										duration: 3000
									});
									return;
								}									
							},							
						})
					}
				}else{
					uni.showToast({
						title: '请正确扫描编码',
						icon: 'none',
						duration: 3000
					});
					return;
				}
			},
			
			
			viewScan(codeStr){
				if(!this.userflag){
					return;
				}
				var result = common.scanCode(codeStr);
				if(result[0] == "C"){
					var tempList = [];
					for(var i=0;i<this.productList.length;i++){
						if(result[1] != this.productList[i].boxCode){
							tempList.push(result[1]);
						}					
					}
					this.productList = tempList;
					this.userflag = false;
				}else{
					uni.showToast({
						title: '请正确扫描编码',
						icon: 'none',
						duration: 3000
					});
					return;
				}				
			},
			
			reduceData(){
				if(this.userflag){
					this.userflag = false;
				}else{
					this.userflag = true;
				}
			},
			
			submit(){
				var that = this;
				uni.request({
					url:that.baseUrl+'/system/TransferOut/addout',
					data:{
						userName:that.userName,
						productList:that.productList	,
						orgId:that.orgId,
						receiptNot:"调出",
					},
					method: 'POST',
					header:{
						'authorization':  getToken()
					},
					dataType:'json',
					success: (res) =>{								
						if(res.data.code == '200' || res.data.code == 200) {
							uni.showToast({
								title: '操作成功',
								icon: 'success',
								duration: 3000
							});
							//that.unregisterScan();
							setTimeout(function(){
								uni.navigateBack({  //uni.navigateTo跳转的返回，默认1为返回上一级
									delta: 1
								});
							},1000);	
						}else{
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 3000
							});
							return;
						}									
					},							
				})
			},
			
			prompt(type){
				var innerAudioContext = uni.createInnerAudioContext();
				innerAudioContext.autoplay = true;
				innerAudioContext.src = '/static/mp3/success.mp3';
				//innerAudioContext.play();//铃声文件的路径
				//innerAudioContext.stop();
				innerAudioContext.onPlay(() => {
				  console.log('开始播放');
				});
				innerAudioContext.onError((res) => {
				  console.log(res.errMsg);
				  console.log(res.errCode);
				});
				innerAudioContext.onPause(function() {  
					console.log('end');  
					innerAudioContext.destroy()  
				})  					
			},
		}
	}
</script>

<style lang="scss">
	.main {
		background-color: #fff;
		height: 300upx;
		align-items: center;
		margin-top: 10upx;
	}
	.text-content {
		font-size: 48rpx;
		text-align: center;
	}
	
	.city {
		/* #ifndef APP-PLUS-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
		// width: 160rpx;
		margin-left: 4px;
		font-size: 26rpx;
	}
	
	.input-view {
		display: flex;
		flex-direction: row;
		flex: 1;
		//background-color: #f8f8f8;
		height: 30px;
		padding: 0 80px;
		flex-wrap: nowrap;
		margin: 7px 0;
		line-height:30px;
		font-size: 26rpx;
	}
	page {
		width: 100%;
		height: 100%;
	}
	
	
	.scrool-page {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 100%;
	}
	
	.header-cont {
		width: 100%;
		height: 110rpx;
		line-height: 100rpx;
		text-align: center;
		// background: red;
	}
	
	.list-cont {
		flex: 1;
		position: relative;
	
		.scrool-more {
			position: absolute;
			left: 0;
			right: 0;
			top: 0;
			bottom: 0;
		}
	
		.list {
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			background: #e0e0e0;
	
		}
	}
	
	
	.footer {
		color: #fff;
		line-height: 100rpx;
		flex: 0 0 100rpx;
		/* 不放大不缩小固定100rpx */
	}
</style>


